﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Mvc;
using Webcar.Utils;
using System.Collections;
using DevExpress.Data;
using DevExpress.Data.Linq;
using System.Drawing;
using DevExpress.Web.ASPxPopupControl;

namespace Webcar.BLL
{
    
    public static class DevControlHelper
    {
        static DevControlHelper()
        {
        }
        public static GridViewExtension BindEF(this GridViewExtension extentions, EventHandler<LinqServerModeDataSourceSelectEventArgs> selectingMethod)
        {
            return extentions.BindToEF(string.Empty, string.Empty, selectingMethod);
        }
        public static void Bind(this GridViewModel gridViewModel, IQueryable query, string sort, ColumnSortOrder order)
        {
            gridViewModel.ProcessCustomBinding(
                (e) => { e.DataRowCount = query.ApplyFilter(e.FilterExpression).Count(); },
                (e) =>
                {
                    e.Data = query.ApplyFilter(e.State.FilterExpression)
                    .ApplyFilter(e.GroupInfoList)
                    .ApplySorting(e.State, sort, order)
                    .Skip(e.StartDataRowIndex)
                    .Take(e.DataRowCount);
                },
                (e) =>{
                    var query2 = query
                    .ApplyFilter(e.State.FilterExpression)
                    .ApplyFilter(e.GroupInfoList);
                    var list = new ArrayList();
                    foreach (var item in e.SummaryItems)
                    {
                        switch (item.SummaryType)
                        {
                            case SummaryItemType.Count:
                                list.Add(query2.Count());
                                break;
                            case SummaryItemType.Sum:
                                list.Add(query2.Sum(item.FieldName));
                                break;
                        }
                    }
                    e.Data = list;
                },
                (e) => {
                    e.Data = query
                    .ApplyFilter(e.State.FilterExpression)
                    .ApplyFilter(e.GroupInfoList)
                    .GetGroupInfo(e.FieldName, e.SortOrder);
                },
                (e) =>
                {
                    query.ApplyFilter(e.FilterExpression)
                    .UniqueValuesForField(e.FieldName);
                }
            );
        }

        public static GridViewModel CreateGridViewModel(string viewName,string key_field)
        {
            var viewModel = GridViewExtension.GetViewModel(viewName);
            if (viewModel == null)
            {
                viewModel = new GridViewModel();
                viewModel.Pager.PageSize = 20;
                viewModel.KeyFieldName = key_field;
                
            }
            return viewModel;
        }
        public static void InitDefaultSettings(this PopupControlSettings settings)
        {
            settings.AllowDragging = true;
            settings.CloseAction = CloseAction.CloseButton;
            settings.PopupAnimationType = AnimationType.None;
            settings.Modal = true; 
            //settings.EnableCallbackAnimation = true;
            settings.ShowLoadingPanel = false;
            settings.PopupHorizontalAlign = PopupHorizontalAlign.WindowCenter;
            settings.PopupVerticalAlign = PopupVerticalAlign.WindowCenter;
            settings.LoadContentViaCallback = LoadContentViaCallback.OnFirstShow;
            settings.ClientSideEvents.BeginCallback = "OnBeginCallback";
            settings.ClientSideEvents.EndCallback = "OnEndCallback";
        }
        public static void EnableAlternatingRowColor(this GridViewSettings settings)
        {
            settings.Styles.AlternatingRow.BackColor = Color.FromName("#e7f0fa");
        }
        public static void EnableRowIndexColumn(this GridViewSettings settings)
        {
            var col = settings.Columns.Add("xh", "序号");
            col.UnboundType = DevExpress.Data.UnboundColumnType.String;
            col.CellStyle.HorizontalAlign = HorizontalAlign.Center;
            col.Width = Unit.Pixel(30);
            settings.CustomColumnDisplayText += (s, e) =>
            {
                if (e.Column.FieldName == "xh" && e.VisibleRowIndex >= 0)
                {
                    e.DisplayText = (e.VisibleRowIndex + 1).ToString();
                }
            };
        }
        public static void InitDefaultPagerGridViewSettings(this GridViewSettings settings)
        {
            settings.SettingsLoadingPanel.Mode = GridViewLoadingPanelMode.Disabled;
            settings.SettingsPager.Position = PagerPosition.Bottom;
            
            settings.SettingsPager.FirstPageButton.Visible = true; 
            settings.SettingsPager.LastPageButton.Visible = true;
            settings.SettingsPager.PageSizeItemSettings.Visible = true;
            settings.SettingsPager.PageSizeItemSettings.Items = new string[] { "10", "20", "30" };
            settings.SettingsPager.PageSize = 15;
            settings.Height = System.Web.UI.WebControls.Unit.Percentage(100);
            settings.Width = System.Web.UI.WebControls.Unit.Percentage(99.8);
            settings.Settings.VerticalScrollBarStyle = GridViewVerticalScrollBarStyle.Standard;
            settings.Settings.HorizontalScrollBarMode = ScrollBarMode.Hidden;
            settings.SettingsBehavior.AllowDragDrop = false; 
            settings.SettingsPager.Summary.Text = "第{0}页，总共{1}页 ({2}条记录).";
            //settings.EnableCallbackAnimation = true; 
            //settings.ClientSideEvents.Init = "function(s, e) { s.PerformCallback(); }";
            //settings.ClientSideEvents.BeginCallback = "function(s, e) {  }";
            //settings.ClientSideEvents.EndCallback = "function(s, e) {  }";
            

            settings.Settings.ShowFilterRow = false;
            settings.Settings.ShowFilterRowMenu = false;
            settings.CommandColumn.Visible = false;
            settings.CommandColumn.Width = 20;
            settings.CommandColumn.ClearFilterButton.Visible = false;

            

        }
        public static void AddFileKindColumn(this GridViewSettings settings, string fieldname = "Kind", string caption = "文件类型")
        {
            var col = GetComboxColumn(fieldname, caption, WebcarDataHelper.FileKind.Union(WebcarDataHelper.LawKind).ToList());
            settings.Columns.Add(col);
        }
        public static void AddClassLevelColumn(this GridViewSettings settings, string fieldname = "class_level", string caption = "技能等级")
        {
            var col = GetComboxColumn(fieldname, caption, WebcarDataHelper.ClassLevel);
            settings.Columns.Add(col);
        }
        public static void AddChildDeptColumn(this GridViewSettings settings, string fieldname = "CHILD_DEPT", string caption = "详细部门")
        {
            var col = GetComboxColumn(fieldname, caption, WebcarDataHelper.AllDeptList);
            settings.Columns.Add(col);
        }
        public static void AddJobKindColumn(this GridViewSettings settings, string fieldname = "JOB_KIND", string caption = "用工类别")
        {
            var col = GetComboxColumn(fieldname, caption, WebcarDataHelper.JobKind);
            settings.Columns.Add(col);
        }
        public static void AddRepairKindColumn(this GridViewSettings settings, string fieldname = "repair_kind", string caption = "维修类型")
        {
            var col = GetComboxColumn(fieldname, caption, WebcarDataHelper.RepairKind);
            settings.Columns.Add(col);
        }

        public static void AddSexKindColumn(this GridViewSettings settings, string fieldname = "SEX", string caption = "性别")
        {
            var col = GetComboxColumn(fieldname, caption, WebcarDataHelper.SexKind);
            settings.Columns.Add(col);
        }

        public static void AddDeptColumn(this GridViewSettings settings, string fieldname = "DEPT_CODE", string caption = "所属部门")
        {
            var col = GetComboxColumn(fieldname, caption, WebcarDataHelper.AllDeptList);
            settings.Columns.Add(col);
        }

        public static void AddCarSortColumn(this GridViewSettings settings, string fieldname = "CAR_SORT", string caption = "车型")
        {
            var col = GetComboxColumn(fieldname, caption, WebcarDataHelper.CarSortCode);
            settings.Columns.Add(col);
        }
        public static MVCxGridViewColumn AddDeptKindColumn(this GridViewSettings settings, string fieldname = "dept_kind", string caption = "单位类型")
        {
            var col = GetComboxColumn(fieldname, caption, WebcarDataHelper.DeptKindCode);
            settings.Columns.Add(col);
            return col;
        }
        public static void AddDriverStatusColumn(this GridViewSettings settings, string fieldname = "IS_USE", string caption = "状态")
        {
            var col = GetComboxColumn(fieldname, caption, WebcarDataHelper.DriverStatusCode);
            settings.Columns.Add(col);
        }
        public static void AddCarStatusColumn(this GridViewSettings settings, string fieldname = "IS_USE", string caption = "状态")
        {
            var col = GetComboxColumn(fieldname, caption, WebcarDataHelper.CarStatusCode);
            settings.Columns.Add(col);
        }
        public static void AddSecTypeColumn(this GridViewSettings settings, string fieldname = "sec_type", string caption = "事故程度")
        {
            var col = GetComboxColumn(fieldname, caption, WebcarDataHelper.SecType);
            settings.Columns.Add(col);
        }
        public static void AddSecDutyColumn(this GridViewSettings settings, string fieldname = "sec_duty", string caption = "事故责任")
        {
            var col = GetComboxColumn(fieldname, caption, WebcarDataHelper.SecDuty);
            settings.Columns.Add(col);
        }

        public static void AddSecKindColumn(this GridViewSettings settings, string fieldname = "sec_kind", string caption = "事故类型")
        {
            var col = GetComboxColumn(fieldname, caption, WebcarDataHelper.SecKind);
            settings.Columns.Add(col);
        }
        public static void AddYesNoColumn(this GridViewSettings settings, string fieldname, string caption)
        {
            var col = GetComboxColumn(fieldname, caption, WebcarDataHelper.YesNoCode);
            settings.Columns.Add(col);
        }
        public static void AddYesNoColumn2(this GridViewSettings settings, string fieldname , string caption)
        {
            var col = GetComboxColumn(fieldname, caption, WebcarDataHelper.YesNoCode2);
            settings.Columns.Add(col);
        }

        public static void AddLeaderCarStatusColumn(this GridViewSettings settings, string fieldname, string caption)
        {
            List<Tuple<string, string>> list = new List<Tuple<string, string>>();
            list.Add(new Tuple<string, string>("1", "固定"));
            list.Add(new Tuple<string, string>("2", "相对固定"));
            list.Add(new Tuple<string, string>("3", "未固定"));
            var col = GetComboxColumn(fieldname, caption, list);
            settings.Columns.Add(col);
        }

        public static MVCxGridViewColumn GetComboxColumn(string fieldname,string caption,List<Tuple<string,string>> dictionary)
        {
            MVCxGridViewColumn col = new MVCxGridViewColumn();
            col.FieldName = fieldname;
            col.Caption = caption;
            
            col.ColumnType = MVCxGridViewColumnType.ComboBox;
            var comboBoxProperties = col.PropertiesEdit as ComboBoxProperties;
            comboBoxProperties.DataSource = dictionary;
            comboBoxProperties.TextField = "Item2";
            comboBoxProperties.ValueField = "Item1";
            comboBoxProperties.ValueType = typeof(string);
            return col;
        }
    }
            

    //public class MVCxGridViewColumnNew:MVCxGridViewColumn
    //{
    //    public MVCxGridViewColumnNew():base()
    //    {

    //    }

    //    public MVCxGridViewColumnNew(string fieldname, TypeOfAppCache key)
    //        : base(fieldname)
    //    {
    //        this.DictionaryKey = key;
    //    }
    //    public MVCxGridViewColumnNew(string fieldname, string caption, TypeOfAppCache key)
    //        : base(fieldname, caption)
    //    {
    //        this.DictionaryKey = key;
    //    }
    //    public TypeOfAppCache DictionaryKey
    //    {
    //        get;
    //        set;
    //    }
    //}

}